package com.kuang.design.creation.prototype;

/**
 * 原型模式
 * 用于创建重复的对象，同时又能保证性能
 * 1.myMybatis:操作数据库，从数据库查出很多记录（70%改变很少）
 * 2.每次查数据，查到以后把所有数据都封装一个对象返回
 * 1000 thread：查一个记录 new User("zhangsan","18");每次创建一个多项封装并返回，系统里面就会有1000个User；浪费内存
 * 3.缓存:查过的保存
 */
public class MainTest {
    public static void main(String[] args) throws CloneNotSupportedException {
        MyMybatis myMybatis = new MyMybatis();

        //每次使用都获取一次user, 造成多次获取同一个对象
        User zhangsan = myMybatis.getUser("zhangsan");
        User zhangsan1 = myMybatis.getUser("zhangsan");
        User zhangsan2 = myMybatis.getUser("zhangsan");
        User zhangsan3 = myMybatis.getUser("zhangsan");
        System.out.println("++++++++++++++++++++++++++++++");
        System.out.println("++++++++++++++++++++++++++++++");
        User zhangsan4 = myMybatis.getUserWithCache("zhangsan");
        System.out.println("4==>" + zhangsan4);
        zhangsan4.setUserName("李四");

        System.out.println("4己修改" + zhangsan4);
        User zhangsan5 = myMybatis.getUserWithCache("zhangsan");
        System.out.println("5==>" + zhangsan5);
        zhangsan5.setUserName("李四");

        User zhangsan6 = myMybatis.getUserWithCache("zhangsan");
        System.out.println("6==>" + zhangsan6);


        System.out.println(zhangsan == zhangsan1);
        System.out.println(zhangsan4 == zhangsan1);
        System.out.println(zhangsan4 == zhangsan6);
        System.out.println(zhangsan5 == zhangsan6);
    }
}
